import { createRouter, createWebHistory } from 'vue-router'

// 布局组件
import AppSidebar from '../components/layout/AppSidebar.vue'

// 路由懒加载
const Dashboard = () => import('../views/Dashboard.vue')
const Login = () => import('../views/Login.vue')
const Register = () => import('../views/Register.vue')
const NotFound = () => import('../views/NotFound.vue')
const ProjectList = () => import('../views/project/ProjectList.vue')
const ProjectDetail = () => import('../views/project/ProjectDetail.vue')
const TaskDetail = () => import('../views/task/TaskDetail.vue')
const AtomicTaskList = () => import('../views/task/AtomicTaskList.vue')
const AtomicTaskDetail = () => import('../views/task/AtomicTaskDetail.vue')
const GitTaskDetail = () => import('../views/task/GitTaskDetail.vue')
const DatabaseTaskDetail = () => import('../views/task/DatabaseTaskDetail.vue')
const TaskDependencies = () => import('../views/task/TaskDependencies.vue')
const TaskProgress = () => import('../views/task/TaskProgress.vue')
const TaskExecution = () => import('../views/task/TaskExecution.vue')
const TaskReview = () => import('../views/task/TaskReview.vue')
const BlockedTasks = () => import('../views/task/BlockedTasks.vue')
const FileHistory = () => import('../views/file/FileHistory.vue')
const AIConversation = () => import('../views/ai/AIConversation.vue')
const Notifications = () => import('../views/notification/Notifications.vue')
const UserSettings = () => import('../views/UserSettings.vue')

const routes = [
  {
    path: '/',
    component: AppSidebar,
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: { title: '仪表盘', icon: 'menu', requiresAuth: true }
      },
      {
        path: 'projects',
        name: 'ProjectList',
        component: ProjectList,
        meta: { title: '项目列表', icon: 'folder', requiresAuth: true }
      },
      {
        path: 'projects/:id',
        name: 'ProjectDetail',
        component: ProjectDetail,
        meta: { title: '项目详情', icon: 'document', requiresAuth: true },
        props: true
      },
      {
        path: 'tasks/:id',
        name: 'TaskDetail',
        component: TaskDetail,
        meta: { title: '任务详情', icon: 'document', requiresAuth: true },
        props: true
      },
      {
        path: 'atomic-tasks',
        name: 'AtomicTaskList',
        component: AtomicTaskList,
        meta: { title: '原子任务', icon: 'document', requiresAuth: true }
      },
      {
        path: 'atomic-tasks/:id',
        name: 'AtomicTaskDetail',
        component: AtomicTaskDetail,
        meta: { title: '原子任务详情', icon: 'document', requiresAuth: true },
        props: true
      },
      {
        path: 'git-tasks/:id',
        name: 'GitTaskDetail',
        component: GitTaskDetail,
        meta: { title: 'Git操作任务', icon: 'document', requiresAuth: true },
        props: true
      },
      {
        path: 'database-tasks/:id',
        name: 'DatabaseTaskDetail',
        component: DatabaseTaskDetail,
        meta: { title: '数据库操作任务', icon: 'document', requiresAuth: true },
        props: true
      },
      {
        path: 'tasks/:id/dependencies',
        name: 'TaskDependencies',
        component: TaskDependencies,
        meta: { title: '任务依赖关系', icon: 'document', requiresAuth: true },
        props: true
      },
      {
        path: 'tasks/:id/progress',
        name: 'TaskProgress',
        component: TaskProgress,
        meta: { title: '任务进度', icon: 'time', requiresAuth: true },
        props: true
      },
      {
        path: 'tasks/:id/execution',
        name: 'TaskExecution',
        component: TaskExecution,
        meta: { title: '任务执行', icon: 'video-play', requiresAuth: true },
        props: true
      },
      {
        path: 'executions/:id/review',
        name: 'TaskReview',
        component: TaskReview,
        meta: { title: '任务审核', icon: 'check', requiresAuth: true },
        props: true
      },
      {
        path: 'blocked-tasks',
        name: 'BlockedTasks',
        component: BlockedTasks,
        meta: { title: '阻塞任务', icon: 'document', requiresAuth: true }
      },
      {
        path: 'file-history',
        name: 'FileHistory',
        component: FileHistory,
        meta: { title: '文件历史', icon: 'document', requiresAuth: true }
      },
      {
        path: 'tasks/:id/ai-conversations',
        name: 'AIConversation',
        component: AIConversation,
        meta: { title: 'AI会话记录', icon: 'document', requiresAuth: true },
        props: true
      },
      {
        path: 'notifications',
        name: 'Notifications',
        component: Notifications,
        meta: { title: '通知中心', icon: 'document', requiresAuth: true }
      },
      {
        path: 'settings',
        name: 'UserSettings',
        component: UserSettings,
        meta: { title: '用户设置', icon: 'setting', requiresAuth: true }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { title: '注册' }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFound,
    meta: { title: '404' }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const isAuthenticated = !!token
  
  // 如果路由需要登录但用户未认证
  if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated) {
    next({ name: 'Login' })
  } 
  // 如果用户已认证，但访问登录页
  else if (to.name === 'Login' && isAuthenticated) {
    next({ name: 'Dashboard' })
  } 
  else {
    next()
  }
})

export default router 